import {reqGetIndexList,reqGetPageGoodsList,reqGetChannelInfo} from '../../api/index'
Page({
  data:{
    indexList:{},
    hotSaleBeforeList:[],
    hotSaleAfterList:[],
    categoryList:[],
    itemList:[],
    show:true,
    tabsCategoryItemList:[],
    currentCategory:{},
    size:10,
    status:'more',
    carouselList:[]
  },
  onShow(){
    this.getIndexList()
    this.getGoodsPageList()
  } ,
  //获取首页数据封装函数
  async getIndexList(){
    try {
      const res = await reqGetIndexList()
      const hotSaleBeforeList = res.data.data.categoryHotSellModule.categoryList.filter((item,index)=>index<2)
      const hotSaleAfterList = res.data.data.categoryHotSellModule.categoryList.filter((item,index)=>index>=2)
      this.setData({
        indexList:res.data.data,
        categoryList:res.data.data.categoryHotSellModule.categoryList,
        hotSaleBeforeList,
        hotSaleAfterList,
        carouselList:res.data.data.focusList
      })
    } catch (error) {
        console.log(error)
    }
  },
 //获取商品分页数据
 async getGoodsPageList(){
   let {size,itemList} = this.data
   try {
     this.setData({
       status:'loading'
     })
     const res = await reqGetPageGoodsList(size)
     if(res.data.itemList.length  > 0){
        if(res.data.itemList.length < size){
          this.setData({
            status:'nomore'
          })
        }else{
          this.setData({
            status:'more'
          })
        }
     }else{ 
      this.setData({
        status:'nomore'
      })
     }
    //  itemList.push(...res.data.itemList)
     
     this.setData({
       itemList:res.data.itemList,
       size:size+10
     })
   } catch (err) {
     this.setData({
       status:'error'
     })
     console.log(err)
   }
 },
 // 改变tab的选择状态时触发
 changeSelectHandle(e){
 const categoryId =  this.data.categoryList.reduce((prev,item)=>{
    if(item.extra.operationResource.categoryName === e.detail.title){
      prev = item.extra.operationResource.categoryId
      return prev
    }
    return prev
  },undefined)
  this.getChannelInfo(categoryId)
 },
 // 封装获取频道的相关信息函数
 async getChannelInfo(categoryId){
   try {
   const res = await reqGetChannelInfo(categoryId)
   this.setData({
     tabsCategoryItemList:res.data.categoryItemList,
     currentCategory:res.data.currentCategory
   })
   } catch (error) {
     console.log(error)
   }
 },
 onReachBottom(){
   if(this.data.status === 'more'){
    this.getGoodsPageList()
   }
 }

  
})